<template>
	<div class="aid">
		<div class="base">
			<p class="fixed-top">
				{{ JSON.stringify(text) == '{}' ? '添加预警' : '修改预警' }}
				<a-icon @click="closeFunction" type="close" />
			</p>
			<div class="main" id="new_message">
				<a-form-model ref="ruleForm" :model="form" :rules="rules">
					<a-form-model-item label="名称" prop="Title"><a-input v-model="form.Title" /></a-form-model-item>
					<a-form-model-item label="预警间隔" prop="Type">
						<a-select :allowClear="true" v-model="form.Type"  placeholder="全部" optionFilterProp="children" showSearch>
							<a-select-option v-for="(item, index) in WarningList" :key="index" :value="item.ID">{{ item.Name }}</a-select-option>
						</a-select>
					</a-form-model-item>
					<a-form-model-item  v-if="form.Type=='1'||form.Type=='2'" label="预警日期" prop="SendTime">
						<a-input-number v-model="form.SendTime" :min="1" :max="31" />
					</a-form-model-item>
					<a-form-model-item v-if="form.Type=='3'" label="每年预警日期" prop="SendTime">
						<a-date-picker format="MM/DD" v-model:value="form.SendTime" />
					</a-form-model-item>
					
					<a-form-model-item label="部门" :prop="form.Grade&&form.Grade.length==0&&form.DepartCode&&form.DepartCode.length==0?'DepartCode':''">
						<a-select :allowClear="true"  mode="multiple" v-model="form.DepartCode"  placeholder="请选择部门,如不选择,则默认全部部门" optionFilterProp="children" showSearch>
							<a-select-option v-for="(item, index) in DepartCodeList" :key="index" :value="item.Code">{{ item.Name }}</a-select-option>
						</a-select>
					</a-form-model-item>
					<a-form-model-item label="年级" :prop="form.Grade&&form.Grade.length==0&&form.DepartCode&&form.DepartCode.length==0?'Grade':''">
						<a-select :allowClear="true"  mode="multiple" v-model="form.Grade"  placeholder="请选择年级,如不选择,则默认全部年级" optionFilterProp="children" showSearch>
							<a-select-option v-for="(item, index) in GradeList" :key="index" :value="item.ID">{{ item.Name }}</a-select-option>
						</a-select>
						<!-- <a-input class="field-right" placeholder="请选择年级" v-decorator="[`grade`]" /> -->
						<!-- <a-date-picker
							mode="year"
							placeholder="请选择年级"
							format="YYYY"
							:value="form.Grade"
							:open="yearPickShow"
							@panelChange="handlePanelChange"
							@openChange="handleOpenChange"
							@change="yearChange"
						/> -->
					</a-form-model-item>
					<!-- <a-form-model-item label="描述" prop="comment"><a-input type="textarea" :auto-size="global.textareaHeight" v-model="form.Content" /></a-form-model-item>
					 -->
					<a-form-model-item label="" class="fixed-bottom">
						<a-button type="primary" @click="handleSubmit">{{ JSON.stringify(text) == '{}' ? '确认添加' : '确认修改' }}</a-button>
						<a-button style="margin-left: 10px;" @click="closeFunction">取消</a-button>
					</a-form-model-item>
				</a-form-model>
			</div>
		</div>
	</div>
</template>

<script>
import moment from 'moment';
import { ManageCreditWarningDoAdd, ManageCreditWarningDoUpdate, DoGetDetailStudent,ManageStudentDoGetManageDepartList } from '@/api/follow';
export default {
	props: {
		text: Object,
		ClassList: Array,
	},
	watch: {
		text: function(text) {
			if (JSON.stringify(text) == '{}') {
			} else {
				// this.GetDetailStudent(text)
				this.form = text;
				this.form.DepartCode=text.DepartCode?text.DepartCode.split(','):[];
				this.form.Grade=text.Grade?text.Grade.split(','):[];
				this.gradeYear = text.Grade;
				this.form.Type=text.Type.toString();
				if(this.form.Type!=3){
					this.form.SendTime=(text.SendTime.split('-'))[2]
				}
			}
		}
	},
	data() {
		return {
			GradeList:[],
			WarningList:this.global.WarningList,
			DepartCodeList:[],
			gradeYear: null,
			yearPickShow: false,
			gradeYear: null,
			yearPickShow: false,
			loading: false,
			NowMonth:'',
			formLayout: 'horizontal',
			form: this.$form.createForm(this, {
				name: 'coordinated'
			}),
			form: {
				Title: '',
				Grade:[],
				DepartCode:[],
				SendTime:'',
				Type:'',
			},
			rules: {
				Title: [
					{
						required: true,
						message: '请输入名称',
						trigger: 'change'
					}
				],
				Type: [
					{
						required: true,
						message: '请选择预警间隔',
						trigger: 'change'
					}
				],
				SendTime: [
					{
						required: true,
						message: '请选择预警时间',
						trigger: 'change'
					}
				],
				DepartCode: [
					{
						required: true,
						message: '请选择部门',
						trigger: 'change'
					}
				],
				Grade: [
					{
						required: true,
						message: '请选择年级',
						trigger: 'change'
					}
				],
				IsHideScore: [
					{
						required: true,
						message: '请选择是否隐藏积分',
						trigger: 'change'
					}
				],
				
			}
		};
	},
	created() {
		this.getOtherList()
		this.getNowTime()
		for(let i=1;i<20;i++){
			this.GradeList.push({
				ID:2010+i,
				Name:2010+i+'年级'
			})
		}
		console.log(this.GradeList)
	},
	methods: {
		getNowTime() {
		        var now = new Date();
		        var year = now.getFullYear(); //得到年份
		        var month = now.getMonth(); //得到月份
		        var day = now.getDate(); //得到天
		        if (day < 10) {
		          day = "0" + day
		        }
		        month = month + 1;
		        month = month.toString().padStart(2, "0");
		        /**
		         * 获取月初到月末，年初到年末
		         * **/
		        this.NowMonth=year+'-'+month
		      },
		filterOption(input, option) {
			return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0;
		},
		moment,
		handleOpenChange(status) {
			this.yearPickShow = status;
		},
		handlePanelChange(value, mode) {
			this.form.Grade = moment(value).format('YYYY');
			this.yearPickShow = false;
		},
		yearChange(date, dateString) {
			if (!date) {
				this.form.Grade = null;
			}
		},
		async getOtherList() {
			// let res = await GetClassList();
			// this.ClassList = res.data.data;
			let res1 = await ManageStudentDoGetManageDepartList();
			
			this.DepartCodeList = res1.data.data;
		},
		closeFunction(data) {
			this.$refs.ruleForm.resetFields();
			this.$emit('closeFun', data);
			this.form = {
				Title: '',
				Grade:[],
				DepartCode:[],
				SendTime:'',
				Type:''
			};
			this.gradeYear = null;
			this.fileList = [];
		},
		handleSubmit(e) {
			this.$refs.ruleForm.validate(async valid => {
				if (valid) {
					let data = this.form;
					// data.EndTime = moment(this.form.EndTime).format('YYYY-MM-DD');
					// console.log(data);
					// if(data.StartDay<10){
					// 	data.StartDay='0'+data.StartDay
					// }
					console.log(data);
					if(data.Type!=3){
						data.SendTime='2022-01-'+data.SendTime
					}else{
						data.SendTime= moment(data.SendTime).format('YYYY-MM-DD')
					}
					data.DepartCode=this.form.DepartCode.toString();
					data.Grade=this.form.Grade.toString();
					let res = {};
					if (JSON.stringify(this.text) == '{}') {
						res = await ManageCreditWarningDoAdd(data);
					} else {
						res = await ManageCreditWarningDoUpdate(data);
					}
					if (res.data.code === 0) {
						this.$message.success(res.data.msg);
						this.closeFunction('1');
					} else {
						this.$message.error(res.data.msg);
					}
				} else {
					return false;
				}
			});
		}
	}
};
</script>

<style lang="less" scoped>
/deep/.ant-form-item {
	margin: 20px 50px;
}
/deep/.ant-form-item-label {
	width: 160px !important;
	overflow: hidden;
	white-space: nowrap;
	text-overflow: ellipsis;
	text-align: right;
}
/deep/.ant-form-item-control-wrapper {
	flex: 1;
}
</style>
